<script setup name="Loginlog">
	import {ref, reactive} from 'vue'
	import {
		Delete,
		Edit,
		Message,
		Search,
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {loginlogApi} from "@/api/loginlog.js";
	//表格数据
	let loginlogList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const loginLogQuery = reactive({
		page: 1,
		limit: 10,
		name: '',
		msg: '',
	})
	
	const loadData = () => {
		loginlogApi.selectByPage(loginLogQuery).then(result => {
			loginlogList.value = result.data.list
			total.value = result.data.total
		})
	}
	
	loadData()
	
	const onSearch = () => {
		loginLogQuery.page = 1
		loadData()
	}
	
	const dialogDetailsVisible = ref(false)
	const loginlog = ref({})
	//详情
	const handleClick = (id) => {
		console.log('click')
		loginlogApi.selectById(id).then(result => {
			dialogDetailsVisible.value = true
			loginlog.value = result.data
		})
		
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				loginlogApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				loginlogApi.deleteSelected(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	const clearAll = () => {
		ElMessageBox.confirm(
			'确清除日志吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				loginlogApi.deleteAll().then(result => {
					if (result.code === 0) {
						ElMessage.success(result.msg)
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="warning" @click="clearAll">清空日志</el-button>
					<el-button type="danger" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="操作人员">
					<el-input v-model="loginLogQuery.name" placeholder="请输入操作人员名称" clearable/>
				</el-form-item>
				<el-form-item label="提示信息">
					<el-input v-model="loginLogQuery.msg" placeholder="请输入请求方法" clearable/>
				</el-form-item>
				<el-form-item label="访问时间">
					<el-date-picker
						v-model="loginLogQuery.beginCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
					/>
					-
					<el-date-picker
						v-model="loginLogQuery.endCreateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="loginlogList" border stripe style="width: 100%;font-size:16px"
					  @selection-change="handleSelectionChange" height="450px">
				<el-table-column type="selection" :selectable="selectable" width="60" align="center"/>
				<el-table-column fixed prop="id" label="ID" sortable width="70" align="center"/>
				<el-table-column prop="adminId" label="用户ID" align="center" min-width="100"/>
				<el-table-column prop="adminName" label="用户名" align="center" min-width="120" show-overflow-tooltip/>
				<el-table-column prop="ip" label="登录IP地址" align="center" min-width="120" show-overflow-tooltip/>
				<el-table-column prop="msg" label="提示信息" align="center" min-width="130" show-overflow-tooltip/>
				<el-table-column prop="accessTime" label="访问时间" align="center" min-width="200" show-overflow-tooltip/>
				<el-table-column prop="status" label="状态" align="center" min-width="100" show-overflow-tooltip>
					<template #default="scope">
						<div v-if="scope.row.status == 0">停用</div>
						<div v-if="scope.row.status == 1">正常</div>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间" align="center" width="210" show-overflow-tooltip/>
				<el-table-column fixed="right" label="操作" min-width="150" align="center">
					<template #default="{row}">
						<el-tooltip
							class="box-item"
							effect="dark"
							content="查看详情"
							placement="top"
						>
							<el-button type="info" :icon="Message" circle @click="handleClick(row.id)"/>
						</el-tooltip>
						<el-tooltip
							class="box-item"
							effect="dark"
							content="单条删除"
							placement="top"
						>
							<el-button type="danger" :icon="Delete" circle @click="deleteById(row.id)"/>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="loginLogQuery.page"
				v-model:page-size="loginLogQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
	</div>
	<!--详情-->
	<el-dialog v-model="dialogDetailsVisible" title="日志详情" style="width: 70%;font-size:16px" :lock-scroll="false">
		<el-descriptions border style="font-size:32px" :data="loginlog">
			<el-descriptions-item label="访问ID" style="font-size: 20px" align="center">{{loginlog.id }}</el-descriptions-item>
			<el-descriptions-item label="用户ID" align="center">{{ loginlog.adminId }}</el-descriptions-item>
			<el-descriptions-item label="用户名" align="center">{{ loginlog.adminName }}</el-descriptions-item>
			<el-descriptions-item label="登录IP地址" align="center">{{ loginlog.ip }}</el-descriptions-item>
			<el-descriptions-item label="提示信息" align="center">{{ loginlog.msg }}</el-descriptions-item>
			<el-descriptions-item label="访问时间" align="center">{{ loginlog.accessTime }}</el-descriptions-item>
			<el-descriptions-item label="状态" align="center">
				<template #default>
					<div v-if="loginlog.status == 0">停用</div>
					<div v-if="loginlog.status == 1">正常</div>
				</template>
			</el-descriptions-item>
			<el-descriptions-item label="创建时间" align="center">{{ loginlog.createTime }}</el-descriptions-item>
			<el-descriptions-item label="更新时间" align="center">{{ loginlog.updateTime }}</el-descriptions-item>
			<el-descriptions-item label="操作" align="center" :span="3">
				<el-button type="primary" :icon="Edit" round @click="dialogDetailsVisible = false">关闭</el-button>
			</el-descriptions-item>
		</el-descriptions>
	</el-dialog>
</template>

<style scoped>

</style>
<style>
	.tooltip-base-box {
		width: 600px;
	}
	
	.tooltip-base-box .row {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.tooltip-base-box .center {
		justify-content: center;
	}
	
	.tooltip-base-box .box-item {
		width: 110px;
		margin-top: 10px;
	}
</style>